package com.yxy.controller.admin;

import com.yxy.common.PageResult;
import com.yxy.common.Result;
import com.yxy.constant.RequestMapperConstant;
import com.yxy.pojo.dto.AdminDTO;
import com.yxy.pojo.dto.AdminLoginDTO;
import com.yxy.pojo.dto.AdminPageQueryDTO;
import com.yxy.pojo.entity.Admin;
import com.yxy.pojo.vo.AdminLoginVO;
import com.yxy.pojo.vo.AdminVO;
import com.yxy.properties.JwtProperties;
import com.yxy.service.AdminService;
import com.yxy.utils.AreaUtil;
import com.yxy.utils.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping(RequestMapperConstant.ADMIN_REQUEST_MAPPER + "administrator")
@Slf4j
public class AdminController {

    @Autowired
    private JwtProperties jwtProperties;
    @Autowired
    private AdminService adminService;

    /**
     * 管理员登陆
     *
     * @param adminLoginDTO
     * @return
     */
    @PostMapping("/login")
    public Result<AdminLoginVO> login(@RequestBody AdminLoginDTO adminLoginDTO) {
        log.info("管理员登陆：{}", adminLoginDTO);
        Admin admin = adminService.login(adminLoginDTO);

        // 生成jwt令牌
        HashMap<String, Object> claims = new HashMap<>();
        claims.put("a_id", admin.getId());
        String token = JwtUtil.createJwt(jwtProperties.getAdminSecretKey(), jwtProperties.getAdminExpire(), claims);
        String ip = AreaUtil.getIp();
        String area = AreaUtil.getArea(ip);
        AdminLoginVO vo = AdminLoginVO.builder()
                .id(admin.getId())
                .nickname(admin.getNickname())
                .ip(ip)
                .area(area)
                .token(token)
                .build();
        return Result.success("登陆成功", vo);
    }

    /**
     * 验证管理员token
     *
     * @param token
     * @return
     */
    @GetMapping("/checkAdminToken")
    public Result checkAdminToken(@RequestParam String token) {
        try {
            JwtUtil.parseJwt(jwtProperties.getAdminSecretKey(), token);
            log.info("checkAdminToken：有效");
        } catch (Exception e) {
            log.info("checkAdminToken：无效");
            return Result.error(401, "expired");
        }
        return Result.success("", null);
    }

    /**
     * 获取管理员信息
     *
     * @param id
     * @return
     */
    @GetMapping("/getAdminInfoById")
    public Result<AdminVO> getAdminInfoById(int id) {
        log.info("获取管理员信息：{}", id);
        Admin admin = adminService.getAdminInfoById(id);
        AdminVO vo = new AdminVO();
        BeanUtils.copyProperties(admin, vo);
        return Result.success("", vo);
    }

    /**
     * 分页查询管理员信息
     *
     * @param adminPageQueryDTO
     * @return
     */
    @GetMapping("/pageQuery")
    public Result<PageResult> pageQuery(AdminPageQueryDTO adminPageQueryDTO) {
        log.info("分页查询管理员信息：{}", adminPageQueryDTO);
        PageResult pageResult = adminService.pageQuery(adminPageQueryDTO);
        return Result.success("", pageResult);
    }

    /**
     * 批量删除管理员
     *
     * @param ids
     * @return
     */
    @DeleteMapping("/deleteByIds")
    public Result batchDelete(@RequestParam List<Integer> ids) {
        log.info("批量删除管理员：{}", ids);
        adminService.batchDelete(ids);
        return Result.success("删除成功");
    }

    /**
     * 修改管理员信息
     *
     * @param adminDTO
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestBody AdminDTO adminDTO) {
        log.info("修改管理员信息：{}", adminDTO);
        adminService.update(adminDTO);
        return Result.success("修改成功");
    }

    /**
     * 修改管理员状态
     *
     * @return
     */
    @PutMapping("/update/{id}/{status}")
    public Result updateStatus(@PathVariable int id, @PathVariable int status){
        log.info("修改管理员状态：{}, {}", id, status);
        adminService.updateStatus(id, status);
        return Result.success("", null);
    }

    /**
     * 添加管理员
     * @param adminDTO
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody AdminDTO adminDTO){
        log.info("添加管理员信息：{}", adminDTO);
        adminService.add(adminDTO);
        return Result.success("添加成功");
    }
}
